<?php
function core_app_route() {
	//路由处理
	$request_uri = str_replace('/index.php', '', $_SERVER['REQUEST_URI']);
	$uri = explode('/', parse_url($request_uri, PHP_URL_PATH));
	$module = 'home';
	if (isset($uri[1]) && !empty($uri[1])) {
		//如果是p开头的，则转发到post模块进行处理。
		$module = ($uri[1] == 'p') ? 'post' : $uri[1];

		if ($module == APP_ADMIN_URI) {
			require_once APP_PATH . '/admin/index.php';
			if (function_exists('run_admin_app')) {
				call_user_func('run_admin_app', $uri);
			}
			return;
		}
		if (file_exists(APP_PATH . '/' . $module . '/index.php')) {
			if ($module == 'admin') {
				//禁止admin直接进入
				show_not_found();
				return;
			}
			//模块存在则，直接显示
			require_once APP_PATH . '/' . $module . '/index.php';
			if (function_exists('run_' . $module . '_app')) {
				call_user_func('run_' . $module . '_app', $uri);
			} else {
				show_not_found();
			}
		} else {
			//否则当作page处理
			require_once APP_PATH . '/page/index.php';
			if (function_exists('run_page_app')) {
				call_user_func('run_page_app', $uri);
			} else {
				show_not_found();
			}
		}
	} else {
		require_once APP_PATH . '/home/index.php';
		if (function_exists('run_home_app')) {
			call_user_func('run_home_app', $uri);
		} else {
			show_not_found();
		}
	}
}
/**
 * 过滤只能是字母和数字
 * @param  [type]
 * @return [type]
 */
function filter_data($input_data) {
	if (empty($input_data)) {
		return '';
	}
	if (is_array($input_data)) {
		return '';
	}
	preg_match_all('/[a-zA-Z0-9]/u', $input_data, $res);
	return join('', $res[0]);
}
/**
 * 过滤生成路径
 * @param  string
 * @return [type]
 */
function filter_build_path($build_path = '') {
	if (empty($build_path)) {
		return false;
	}
	$html_file_dir = dirname($build_path);
	//写入文件
	if (!file_exists($html_file_dir)) {
		mkdir($html_file_dir, 0777, true);
	}
	$build_path = str_replace('//', '/', $build_path);
	return $build_path;
}
/**
 * 显示错误页面
 * @return [type]
 */
function show_not_found() {
	require_once APP_VIEW . '/errors/not_found.php'; //引入模版文件
}
/**
 * 获取菜单
 * @return [type]
 */
function get_menu_menu() {
	$menu_data = include SYSTEM_PATH . '/config/menu.php';
	return $menu_data;
}
/**
 * 获取网站配置
 * @param  array
 * @return [type]
 */
function get_site_data($page_data = array()) {
	$site_data = include SYSTEM_PATH . '/config/site.php';

	$page_data['site_name'] = $site_data['site_name'];
	$page_data['site_url'] = $site_data['site_url'];
	$page_data['site_title'] = $site_data['site_title'];
	$page_data['site_seo_title'] = $site_data['site_seo_title'];
	$page_data['site_icp_number'] = $site_data['site_icp_number'];
	$page_data['site_organization'] = $site_data['site_organization'];

	if (!isset($page_data['page_title'])) {
		if (isset($page_data['title'])) {
			$page_data['page_title'] = $page_data['title'] . '-' . $page_data['site_name'] . ' - ' . $site_data['site_seo_title'];
		} else {
			$page_data['page_title'] = $page_data['site_name'] . ' - ' . $site_data['site_seo_title'];
		}
	}
	if (!isset($page_data['keywords'])) {
		$page_data['keywords'] = $site_data['site_keywords'];
	}
	if (!isset($page_data['description'])) {
		$page_data['description'] = $site_data['site_description'];
	}
	return $page_data;
}

/**
 * 获取IP地址
 * @return [type]
 */
function get_ip() {
	$ip = '未知IP';
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
		return is_ip($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : $ip;
	} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		return is_ip($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $ip;
	} else {
		return is_ip($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : $ip;
	}
}
/**
 * 判断是否为IP地址
 * @param  [type]
 * @return boolean
 */
function is_ip($str) {
	$ip = explode('.', $str);
	for ($i = 0; $i < count($ip); $i++) {
		if ($ip[$i] > 255) {
			return false;
		}
	}
	return preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $str);
}
/**
 * 获取汉字拼音首字母
 * @param  [type]
 * @return [type]
 */
function get_pinyin($str) {
	require_once SYSTEM_PATH . '/libs/Pinyin.class.php'; //引入类
	$p = new Pinyin();
	$out = $p->output($str); //汉字转换拼音
	return $out;
}

/**
 * @param $total          总条数
 * @param $curpage          当前页
 * @param $num               每页条数
 * @param string $args
 * @param string $url
 * @return null|string
 */

function get_front_page_navigation($total, $curpage, $num, $url = "") {
	$total_page = ceil($total / $num);

	if ($total_page == 0) {
		$total_page = 1;
		$curpage = 1;
	}

	if ($total_page == 1) {
		return "";
	}
	$output = null;

	$output .= " <ul class=\"spage pagination no-margin\">";

	if ($curpage > 1) {
		$output .= '<li><a href="' . $url . '/index.html" title="首页">首页</a></li>';
		$output .= '<li><a href="' . $url . '/' . ($curpage - 1) . '.html" title="上一页">上一页</a></li>';
	} else {
		$output .= '<li class="disabled"><a href="' . $url . '/index.html" title="首页">首页</a></li>';
		$output .= '<li class="disabled"><a href="' . $url . '/' . ($curpage - 1) . '.html" title="上一页">上一页</a></li>';
	}

	$start = floor($curpage / 10) * 10;
	$end = $start + 9;
	if ($start < 1) {
		$start = 1;
	}

	if ($end > $total_page) {
		$end = $total_page;
	}

	$output .= " ";
	for ($i = $start; $i <= $end; $i++) {
		$output .= ($i != 1 && $i % 10) ? " " : "";

		if ($curpage == $i) {
			$output .= '<li class="active"><a href="#" onclick="return false;" class="this-page">' . $i . '</a></li>'; //输出当前页数
		} else {
			$output .= '<li><a href="' . $url . '/' . ($i) . '.html">' . $i . '</a></li>'; //输出页数
		}
	}

	if ($curpage < $total_page) {
		$output .= '<li><a href="' . $url . '/' . ($curpage + 1) . '.html"title="下一页">下一页</a></li>';
		//$output .= '<li><a href='. $url .'page='.($total_page).$args .' title="末页">末页</a></li>';
	} else {
		// $output .= '<li class="disabled"><a href="' . $url . '/' . ($curpage + 1) . '.html" title="下一页">下一页</a></li>';
		$output .= '<li class="disabled"><a href="' . $url . '/' . ($total_page) . '.html" title="末页">末页</a></li>';
	}

	return $output;
}

if (!function_exists('_error_handler')) {
	function _error_handler($severity, $message, $filepath, $line) {
		echo "<br/>SEVERITY" . $severity . "<br/>MESSGE:" . $message . "<br/>FILE_PATH:" . $filepath . "<br/>LINE:" . $line;

		exit(1); // EXIT_ERROR

	}
}

if (!function_exists('_exception_handler')) {
	function _exception_handler($exception) {
		echo 'Exception: ' . $exception->getMessage() . $exception->getFile() . $exception->getLine();
		exit(1); // EXIT_ERROR
	}
}